package com.ruoyi.ldzlsc.controller;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ldzlsc.domain.vo.ScProductionPlanVo;
import com.ruoyi.ldzlsc.domain.vo.ScProductionPlanDetailVo;
import com.ruoyi.ldzlsc.domain.vo.ScMaterialVo;
import com.ruoyi.ldzlsc.service.IScProductionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
import java.util.Map;

/**
 * 生产计划Controller
 *
 * @author ruoyi
 * @date 2025-06-25
 */
@RestController
@RequestMapping("/sc/productionPlan")
public class ScProductionPlanController extends BaseController {
    @Autowired
    private IScProductionPlanService productionPlanService;

    /**
     * 查询生产计划列表
     */
    //@RequiresPermissions("sc:productionPlan:list")
    @GetMapping("/list")
    public TableDataInfo list(ScProductionPlanVo scProductionPlanVo, @RequestParam Map<String, Object> params) {
        if (params.containsKey("orderSource")) {
            scProductionPlanVo.setSourceType(params.get("orderSource").toString());
        }
        startPage();
        List<ScProductionPlanVo> list = productionPlanService.selectScProductionPlanList(scProductionPlanVo);
        return getDataTable(list);
    }

    /**
     * 分解生产计划
     *
     * @param planCode 生产计划编号
     * @return 操作结果
     */
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:productionPlan:decompose")
    @PostMapping("/decompose/{planCode}")
    public AjaxResult decompose(@PathVariable("planCode") String planCode) {
        productionPlanService.decomposePlan(planCode);
        return AjaxResult.success("分解指令已发送");
    }
    
    /**
     * 根据计划编号获取生产计划详情
     */
    //@RequiresPermissions("sc:productionPlan:query")
    @GetMapping("/{planCode}")
    public AjaxResult getInfo(@PathVariable("planCode") String planCode) {
        return success(productionPlanService.selectScProductionPlanByPlanCode(planCode));
    }
    
    /**
     * 根据计划编号获取生产产品列表
     */
    //@RequiresPermissions("sc:productionPlan:query")
    @GetMapping("/list-products/{planCode}")
    public AjaxResult listProducts(@PathVariable("planCode") String planCode) {
        List<ScProductionPlanDetailVo> list = productionPlanService.selectPlanDetailListByPlanCode(planCode);
        return success(list);
    }

    /**
     * 根据计划编号获取物料明细列表
     */
    //@RequiresPermissions("sc:productionPlan:query")
    @GetMapping("/list-materials/{planCode}")
    public AjaxResult listMaterials(@PathVariable("planCode") String planCode) {
        List<ScMaterialVo> list = productionPlanService.selectMaterialListByPlanCode(planCode);
        return success(list);
    }
    
    /**
     * 新增生产计划
     */
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    //@RequiresPermissions("sc:productionPlan:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ScProductionPlanVo scProductionPlanVo) {
        return toAjax(productionPlanService.insertProductionPlan(scProductionPlanVo));
    }

    /**
     * 修改生产计划
     */
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("sc:productionPlan:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody ScProductionPlanVo scProductionPlanVo) {
        return toAjax(productionPlanService.updateProductionPlan(scProductionPlanVo));
    }

    /**
     * 删除生产计划
     */
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
    //@RequiresPermissions("sc:productionPlan:remove")
    @DeleteMapping("/{productionPlanIds}")
    public AjaxResult remove(@PathVariable Long[] productionPlanIds) {
        return toAjax(productionPlanService.deleteProductionPlanByIds(productionPlanIds));
    }

    /**
     * 根据生产订单创建生产计划
     */
    //@RequiresPermissions("sc:productionPlan:add")
    @PostMapping("/create-from-order/{productionOrderId}")
    public AjaxResult createFromOrder(@PathVariable Long productionOrderId) {
        ScProductionPlanVo planVo = productionPlanService.createPlanFromProductionOrder(productionOrderId);
        return success(planVo);
    }
}